<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>咨询测算查询</title>
		<%include("/common/include.html"){}%>
		<link rel="stylesheet" href="${cxt!}/sb/plugins/ztree/css/zTreeStyle/zTreeStyle.css" type="text/css">
	</head>
	<body class="easyui-layout">
			<div data-options="region:'west',split: true,title:'快速查询'" style="width:240px;padding:10px;">
				<form id="productStockStatisticFrom">
					<div style="margin-bottom: 10px">
						<label class="label-top" style="width: 100%; display: block;">开始时间</label>
						<input name="_query.startTime" value='${date()}' class="easyui-datebox theme-textbox-radius" style="height:32px;width:212px;text-align:center"/>
					</div>
					<div style="margin-bottom: 10px">
						<label class="label-top" style="width: 100%; display: block;">结束时间</label>
						<input name="_query.endTime" value='${date()}' class="easyui-datebox theme-textbox-radius" style="height:32px;width:212px;text-align:center"/>
					</div>
					<div style="margin-bottom: 10px">
						<label class="label-top" style="width: 100%; display: block;">代理商/门店</label>
						<input id="organ" type="text" readonly style="width: 200px; height: 32.4px; padding-left: 10px;" onclick="showMenu(); return false;" class="textbox" value="${curOrgan.name! }"/>
						<input name="_query.selOrganIds" id="organIds" type="hidden" value="${curOrgan.ids! }">
					</div>
					<div style="margin-bottom: 10px">
						<label class="label-top" style="width: 100%; display: block;">产品类别</label>
						<input  id="proType_combobox" name="_query.productType" class=" easyui-combobox  theme-textbox-radius" style="height:32px;width:212px;text-align:center"/>
					</div>
					<div style="margin-bottom: 10px">
						<label class="label-top" style="width: 100%; display: block;">产品名称</label>
						<input  id="pro_combobox" name="_query.productIds" class=" easyui-combobox theme-textbox-radius" style="width:212px;height:32px;text-align:center"/>
					</div>
					<div style="margin-bottom: 10px">
						<a href="#" onclick='searchFrom()' class="easyui-linkbutton" data-options="iconCls:'icon-search'" style="width:212px">搜索</a>
					</div>
					</form>
					<div id="menuContent" class="menuContent" style="display: none; position: absolute;">
						<ul id="organSelectTree" class="ztree" style="margin-top: 0;width: 200px;"></ul>
					</div>
	   	 	</div>
	   	 	<div data-options="region:'center',border: false" style="overflow-x:hidden;">
				<div class="easyui-layout" data-options="fit:true,border: false" style="overflow-x:hidden">
					<div id="table_div" data-options="region:'north',split: true,border: false" style="height:60%;overflow-x:hidden">
						<table id="productStockTable" style="width:100%;height:100%;">
						</table>
					</div>
					<div data-options="region:'center',border: false">
					<table id="productStockHistoryTable" style="height:100%;"></table>
					</div>
				</div>
	    	</div>
		    
		<script type="text/javascript">
			var $form = $('#productStockStatisticFrom');
			function searchFrom(){
				$("#productStockTable").datagrid('load', sy.serializeObject($form));
			}
			$(function() {
				initProTypeSelect();
			})
			//初始化产品类型下拉框
			function initProTypeSelect(){
				var url = "${cxt!}/common/getProductTypes";
				$.post(url, function(data) {
					data.unshift({  
	                     'ids': '-1',  
	                     'name': '-请选择-'  
	                 });
					$('#proType_combobox').combobox({
						 data:data,
					    valueField:'ids',
					    textField:'name',
					    onLoadSuccess : function(){  
					    	 var val = $(this).combobox('getData');  
                                for (var item in val[0]) {  
                                    if (item == 'ids') {  
                                        $(this).combobox('select', val[0][item]);  
                                    }  
                                }
				        },
				        onChange:function(){
				        	initProductSelect();
						}
					});
				});
			}
			//初始化产品下拉框
			function initProductSelect(){
				var typeIds = $("#proType_combobox").val();
				var url = "${cxt!}/common/getProducts?ids=" + typeIds;
				$.post(url, function(data) {
					data.unshift({  
	                     'ids': '-1',  
	                     'name': '-请选择-'  
	                 });
					$('#pro_combobox').combobox({
						 data:data,
					    valueField:'ids',
					    textField:'name',
					    onLoadSuccess : function(){  
					    	 var val = $(this).combobox('getData');  
                                for (var item in val[0]) {  
                                    if (item == 'ids') {  
                                        $(this).combobox('select', val[0][item]);  
                                    }  
                                }
				        }
					});
				});
			}
			function initProductStockTable(orgainIds){
				$('#productStockTable').datagrid({
					title:"当前库存",
					cls: "theme-datagrid",
					toolbar: '#tb',
					singleSelect: true,
					rownumbers: true, //显示序号
					pagination: true,
					pageSize: 10,
					url: "${cxt!}/productStockStatistic/view",
					queryParams: sy.serializeObject($form),
				    method: 'post',
					collapsible: true,
					columns:[[
							{field:'organids',title:'organids',width:130,align:'center',hidden:true},
					  		{field:'productid',title:'productid',width:130,align:'center',hidden:true},
					  		{field:'name',title:'产品名称',width:100,align:'center'},
					  		{field:'typeids',title:'typeids',width:100,align:'center',hidden:true},
					  		{field:'type',title:'产品类型',width:100,align:'center'},
					  		{field:'num',title:'库存量',width:100, align:'center'}
					      ]],
					onLoadSuccess:function(){
						$('#productStockTable').datagrid("selectRow", 0);
						// 获取主表选中行
			        	var gridSelectRow = $("#productStockTable").datagrid('getSelected');
			        	if (typeof(gridSelectRow) == "undefined") { 
			        		$('#productStockHistoryTable').datagrid({ 
			        			cls: "theme-datagrid",
			        			rownumbers: false,
								title: " ",
								columns:[[]]
			        		});
			        	}else{
			        		var productIds = gridSelectRow.productid;
				        	var historyName = gridSelectRow.name + '历史库存';
				        	var param = sy.serializeObject($form);
				        	param["_query.productIds"] = productIds;  
				        	$('#productStockHistoryTable').datagrid({
								cls: "theme-datagrid",
								title: historyName,
								singleSelect: true,
								rownumbers: true, //显示序号
								pagination: true,
								pageSize: 10,
								url: "${cxt!}/productStockStatistic/historyView",
								queryParams: param,
							    method: 'post',
								collapsible: true,
								columns:[[
								  		{field:'organids',title:'organids',width:130,align:'center',hidden:true},
								  		{field:'productid',title:'productid',width:130,align:'center',hidden:true},
								  		{field:'name',title:'产品名称',width:100,align:'center'},
								  		{field:'typeids',title:'typeids',width:100,align:'center',hidden:true},
								  		{field:'type',title:'产品类型',width:100,align:'center'},
								  		{field:'stock',title:'库存量',width:100, align:'center'},
								  		{field:'gxsj',title:'截止日',width:160, align:'center'},
								  		{field:'lookoutin',title:'出入库明细',width:100, align:'center',
								  			formatter: function(value,row,index){
								  				var organids = row.organids;
								  				var productid = row.productid;
								  				var gxsj = row.gxsj;
												return '<a href="#" style="color: blue;" class="button-edit" onclick="lookoutin(\'' + organids + '\',\'' + productid + '\',\'' + gxsj + '\')">查看出入库明细</a>';
											}
								  		}
								      ]]
							});
			        	}
					},
					onClickRow:function(rowIndex,rowData){
						var productIds = rowData.productid;
						var historyName = rowData.name + '历史库存';
						var param = sy.serializeObject($form);
			        	param["_query.productIds"] = productIds; 
						$('#productStockHistoryTable').datagrid({title: historyName});
						$('#productStockHistoryTable').datagrid('load',param);
					}
				});
			}
			function lookoutin(organids,productid,gxsj){
  				var url = "${cxt!}/productStockStatistic/toOutinView?selOrganIds=" + organids + "&productIds=" + productid + "&gxsj=" + gxsj;
				loadViewDialog('出入库明细', url);
			}
		</script>
		<script type="text/javascript">
			var setting = {
				async : {
					enable : true,
					url : '${cxt!}/common/getOrgenTree',
					autoParam : [ "id=ids" ],//, "name=n", "level=lv"
					type : "post"
				},
				view : {
					dblClickExpand : false
				},
				data : {
					simpleData : {
						enable : true
					}
				},
				callback : {
					onClick : onClick,
					onAsyncSuccess : organTreeOnAsyncSuccess
				}
			};
			var flag = 0;
			function organTreeOnAsyncSuccess(event, treeId, msg) {
				
				if(flag == 0){
					var organTree = $.fn.zTree.getZTreeObj("organSelectTree");
					var nodes = organTree.getNodes();
					initProductStockTable(nodes[0].id);
					flag = 1;
				}
			}
			function onClick(e, treeId, treeNode) {
				var zTree = $.fn.zTree.getZTreeObj("organSelectTree");
				var path = "";
				var sNodes = zTree.getSelectedNodes();
				if (sNodes.length > 0) {
					var node = sNodes[0].getPath();
					for(var i =0;i<node.length;i++){
						path += node[i].name + "/"
					}
				}
				var cityObj = $("#organ");
				cityObj.attr("value", path);
				$("#organIds").attr("value", treeNode.id);
				hideMenu();
			}
			function showMenu() {
				var cityObj = $("#organ");
				var cityOffset = $("#organ").offset();
				$("#menuContent").css({
					left : cityOffset.left + "px",
					top : cityOffset.top + cityObj.outerHeight() + "px"
				}).slideDown("fast");
		
				$("body").bind("mousedown", onBodyDown);
			}
			function hideMenu() {
				$("#menuContent").fadeOut("fast");
				$("body").unbind("mousedown", onBodyDown);
			}
			function onBodyDown(event) {
				if (!(event.target.id == "menuBtn"
						|| event.target.id == "menuContent" || $(event.target)
						.parents("#menuContent").length > 0)) {
					hideMenu();
				}
			}
			$.fn.zTree.init($("#organSelectTree"), setting);
		</script>
	</body>
</html>
